976D - Degree Set - CodeForces Solution


constructive algorithms graphs implementation *2500

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#define ff first
#define ss second
#define ll long long
#define ld long double
#define pb push_back
#define mp make_pair
#define sws ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl '\n'
#define teto(a, b) ((a+b-1)/(b))
using namespace std;

// Extra
#define all(x) x.begin(), x.end()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//

const int MAX = 300010;
const ll MOD = (int)1e9 +7;
const int INF = 1e9;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const ld EPS = 1e-8;

void solve(vector<int> d, vector<pair<int,int>>& e) {
    if(d.size() == 1) {
        // assert(d[0] == 1);
        for(int i = 1; i <= d[0]+1; i++) {
            for(int j = i+1; j <= d[0]+1; j++) {
                e.pb({j, i});
            }
        }
        return;
    }
    if(d.size() == 0) {
        return;
    }

    int n = d.size();
    vector<int> d2(n-2);
    for(int i = 1; i < n-1; i++) {
        d2[i-1] = d[i] - d[0];
    }

    solve(d2, e);

    for(int i = d[n-1]+1; i > d[n-1]-d[0]+1; i--) {
        for(int j = 1; j <= d[n-1]+1; j++) {
            if(i <= j) continue;
            e.pb({i, j});
        }
    }
}

int main() {
    sws;
    int n;
    cin >> n;

    vector<int> d(n);
    for(int i = 0; i < n; i++) {
        cin >> d[i];
    }

    vector<pair<int,int>> e;
    solve(d, e);

    cout << e.size() << endl;
    for(auto [a, b] : e) {
        cout << a << " " << b << endl;
    }
    

    return 0;
}


Comments

Submit
0 Comments
More Questions

1080A - Petya and Origami
1642D - Repetitions Decoding
1440A - Buy the String
1658F - Juju and Binary String
478A - Initial Bet
981A - Antipalindrome
365A - Good Number
1204B - Mislove Has Lost an Array
1409D - Decrease the Sum of Digits
1476E - Pattern Matching
1107A - Digits Sequence Dividing
1348A - Phoenix and Balance
1343B - Balanced Array
1186A - Vus the Cossack and a Contest
1494A - ABC String
1606A - AB Balance
1658C - Shinju and the Lost Permutation
1547C - Pair Programming
550A - Two Substrings
797B - Odd sum
1093A - Dice Rolling
1360B - Honest Coach
1399C - Boats Competition
1609C - Complex Market Analysis
1657E - Star MST
1143B - Nirvana
1285A - Mezo Playing Zoma
919B - Perfect Number
894A - QAQ
1551A - Polycarp and Coins